package com.cn.qgx428.orderingmeal.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.myClass.Comment;
import com.cn.qgx428.orderingmeal.myClass.Shop;
import com.cn.qgx428.orderingmeal.myClass.User;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by QinX on 2017/5/24.
 */

public class CommentDialog extends Dialog implements View.OnClickListener {

    private static final String TAG = "CommentDialog";

    private Context context;
    private Shop shop;
    private String userID;

    private EditText commentE;

    public CommentDialog(@NonNull Context context , Shop shop, String userID) {
        super(context);
        this.context = context;
        this.shop = shop;
        this.userID = userID;
        setDialogView();
    }



    private void setDialogView() {
        View view = LayoutInflater.from(context).inflate(R.layout.orderditail_comment_dialog,null);
        TextView shopName = (TextView) view.findViewById(R.id.orderdetail_comment_shopName);
        TextView comment = (TextView) view.findViewById(R.id.orderdetail_comment_comment);
        TextView submit = (TextView) view.findViewById(R.id.orderdetail_comment_submit);
        TextView cancle = (TextView) view.findViewById(R.id.orderdetail_comment_cancel);
        commentE = (EditText) view.findViewById(R.id.orderdetail_comment_comment);

        shopName.setText(shop.getName());
        submit.setOnClickListener(this);
        cancle.setOnClickListener(this);

        setContentView(view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.orderdetail_comment_cancel:
                dismiss();
                break;
            case R.id.orderdetail_comment_submit:
                submitComment();
                break;
        }
    }

    private void submitComment() {
        if (commentE.length() < 5){
            Toast.makeText(context, "评论信息不能少于5个字", Toast.LENGTH_SHORT).show();
        }else {
            Comment comment = new Comment();
            User user = new User();
            user.setObjectId(userID);
            Shop shop = new Shop();
            shop.setObjectId(this.shop.getObjectId());
            comment.setUser(user);
            comment.setShop(shop);
            comment.setContent(commentE.getText().toString());

            comment.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e==null){
                        Toast.makeText(context, "发表成功", Toast.LENGTH_SHORT).show();
                        dismiss();
                    } else {
                        Toast.makeText(context, "服务器错误", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "done: 提交评论信息"+e.getErrorCode()+e.getMessage());
                    }
                }
            });
        }
    }
}
